package com.ybjzo2o.common.utils;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.*;
import com.aliyun.sdk.service.dysmsapi20170525.models.*;
import com.google.gson.Gson;
import darabonba.core.client.ClientOverrideConfiguration;

import java.util.concurrent.CompletableFuture;

/**
 * 短信发送工具类
 */
public class SMSUtils {
    public static final String VALIDATE_CODE = "SMS_302040671";//发送短信验证码
    public static final String ORDER_NOTICE = "SMS_302040671";//体检预约成功通知

    /**
     * 发送短信
     *
     * @param phoneNumbers
     * @param code
     */
    public static void sendShortMessage(String templateCode, String phoneNumbers, String code)  throws Exception{
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId("LTAI5tEvrMp39pKbfyCELdXn")//换成自己的ak
                .accessKeySecret("pevJKTgveFYtdpbqMoGxKDv607bzkH")//换成自己的as
                .build());


        AsyncClient client = AsyncClient.builder()
                .region("cn-hangzhou")
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride("dysmsapi.aliyuncs.com")
                )
                .build();

        SendSmsRequest sendSmsRequest = SendSmsRequest.builder()
                .signName("云宝到家")
                .templateCode(templateCode)
                .phoneNumbers(phoneNumbers)
                .templateParam("{\"code\":\""+code+"\"}").build();

        CompletableFuture<SendSmsResponse> response = client.sendSms(sendSmsRequest);
        SendSmsResponse resp = response.get();
        System.out.println(new Gson().toJson(resp));
        client.close();
    }

    public static void main(String[] args) throws Exception {
        SMSUtils.sendShortMessage("SMS_302040671", "13530026848", "6666");
    }
}